﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CocisDAL;
using CocisModel;
using System.Data;
using CocisWeb.src;

namespace CocisWeb.admin
{
    public partial class ManageRole :BasePage
    {
        public RoleDAL dal = new RoleDAL();
        public LogsDAL logs = new LogsDAL();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                this.BindRole();
            }
        }

        public void BindRole()
        {
            this.gvRole.DataSource = dal.GetAllRoleDataSet();
            this.gvRole.DataBind();
        }
        protected void btnAdd_Click(object sender, EventArgs e)
        {
            string roleName = this.TextBoxNames.Text;
            string roleDesc = this.TextBox1.Text;
            int temp = dal.AddRole(roleName,roleDesc);
            if (temp > 0)
            {
                string Ip = Request.UserHostAddress.ToString();
                UserInfo userInfo = Session["userInfo"] as UserInfo;
                int t = logs.AddNewLogsInfo("insert rolesInfo", userInfo.UserName, Ip);
              
                this.BindRole();
 
            }
        }

        protected void gvRole_RowEditing(object sender, GridViewEditEventArgs e)
        {
            this.gvRole.EditIndex = e.NewEditIndex;
            this.BindRole();
        }

        protected void gvRole_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            this.gvRole.EditIndex = -1;
            this.BindRole();

        }

        protected void gvRole_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            int roleId = int.Parse(this.gvRole.DataKeys[e.RowIndex].Values[0].ToString());
            string roleName = ((TextBox)(this.gvRole.Rows[e.RowIndex].Cells[1].FindControl("txtName"))).Text;
            string roleDesc = ((TextBox)(this.gvRole.Rows[e.RowIndex].Cells[2].FindControl("txtDesc"))).Text;
            int temp = dal.UpdateRole(roleId,roleName,roleDesc);
            if (temp > 0)
            {
                string Ip = Request.UserHostAddress.ToString();
                UserInfo userInfo = Session["userInfo"] as UserInfo;
                int t = logs.AddNewLogsInfo("update roles",userInfo.UserName,Ip);
                this.gvRole.EditIndex = -1;
                this.BindRole();
 
            }
        }

        protected void gvRole_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            int roleId = int.Parse(this.gvRole.DataKeys[e.RowIndex].Values[0].ToString());
            
            int temp = dal.DeleteRole(roleId);
            if (temp > 0)
            {
                string Ip = Request.UserHostAddress.ToString();
                UserInfo userInfo = Session["userInfo"] as UserInfo;
                int t = logs.AddNewLogsInfo("delete roles", userInfo.UserName, Ip);
                
                this.BindRole();

            }
        }

        protected void gvRole_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            //如果是绑定数据行 
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                if (e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate)
                {
                    ((LinkButton)e.Row.Cells[4].Controls[0]).Attributes.Add("onclick", "javascript:return confirm('你确认要删除吗?')");
                }
            } 

        }
    }
}
